import Vue from 'vue'
export default {
  state: {
    id: 0,
    name: '',
    superAdmin: 0,
    superTenant: 0,
    tenantName: '',
    email: '',
    firstName: '',
    lastName: '',

    username: '',
    realName: '',
    headUrl: null,
    gender: 0,
    mobile: '',
    deptId: null,
    status: 1,
    createDate: null,
    tenantCode: '',
    roleIdList: null,
    deptName: null,
    managerId: null,
    timezoneId: null,
    timezoneName: null,
    timezoneOffset: null,
    regSource: null,
    updateSource: null,
    onoff: 'ON',
    tenantId: null,
    psa: null,
    tenantSwitch: false,
    roleList: null,
    cwUser: null,
    // autotaskUrl: null,
    // syncroMspUrl: null,
    // haloUrl: null,
    // cwUrl: null,
    domainUrl:{},
    timeFormat: null,
    integrations: 0,
    tenantList: [],
    tenantTimezoneId: null,
    tenantTimezoneName: null,
    tenantTimezoneOffset: null,
    homePage: null,
    integrationEventType: null,
    microsoftId: null,
    integrationList: [],
    clientRoleList:[]
  },
  mutations: {
    SET_LOGIN_USER_INFO: (state, loginUserInfo) => {
      state.id = loginUserInfo.id
      state.tenantCode = loginUserInfo.tenantCode
      state.timezoneId = loginUserInfo.timezoneId
      state.timezoneName = loginUserInfo.timezoneName
      state.timezoneOffset = loginUserInfo.timezoneOffset
      state.headUrl = loginUserInfo.headUrl
      state.name = loginUserInfo.realName
      state.superAdmin = loginUserInfo.superAdmin
      state.superTenant = loginUserInfo.superTenant
      state.tenantName = loginUserInfo.tenantName
      state.tenantCode = loginUserInfo.tenantCode
      state.email = loginUserInfo.email
      state.firstName = loginUserInfo.firstName
      state.lastName = loginUserInfo.lastName
      state.username = loginUserInfo.username
      state.realName = loginUserInfo.realName
      state.tenantSwitch = loginUserInfo.tenantSwitch
      state.roleList = loginUserInfo.roleList
      state.deptId = loginUserInfo.deptId
      state.cwUser = loginUserInfo.cwUser
      // state.autotaskUrl = loginUserInfo.autotaskUrl
      // state.syncroMspUrl = loginUserInfo.syncroMspUrl
      // state.haloUrl = loginUserInfo.haloUrl
      // state.cwUrl = loginUserInfo.cwUrl
      state.domainUrl = loginUserInfo.domainUrl
      state.timeFormat = loginUserInfo.timeFormat
      state.regSource = loginUserInfo.regSource
      state.tenantList = loginUserInfo.tenantList
      state.tenantTimezoneId = loginUserInfo.tenantTimezoneId
      state.tenantTimezoneName = loginUserInfo.tenantTimezoneName
      state.tenantTimezoneOffset = loginUserInfo.tenantTimezoneOffset
      state.homePage = loginUserInfo.homePage
      state.integrationEventType = loginUserInfo.integrationEventType
      state.microsoftId = loginUserInfo.microsoftId
    },
    SET_TENANT_CODE: (state, tenantCode) => {
      state.tenantCode = tenantCode
    },
    SET_TENANT_NAME: (state, tenantName) => {
      state.tenantName = tenantName
    },
    SET_TIME_FORMAT: (state, timeFormat) => {
      state.timeFormat = timeFormat
    },
    SET_INTEGRATIONS: (state, integrations) => {
      state.integrations = integrations
    },
    SET_CLIENTROLELIST: (state, clientRoleList) => {
      state.clientRoleList = clientRoleList
    }
  },
  actions: {

    getUser ({
      commit
    }) {
      return new Promise((resolve, reject) => {
        Vue.prototype.$interface.login.loginUserInfoApi().then(({
          data: response
        }) => {
          if (response.code !== 0) {
            throw response.msg
          }
          const user = response.data || {}
          commit('SET_LOGIN_USER_INFO', user)

          resolve(user)
        }).catch(error => {
          reject(error)
        })
      })
    },
    setTenantCode ({ commit }, tenantCode) {
      commit('SET_TENANT_CODE', tenantCode)
    },
    setTenantName ({ commit }, tenantName) {
      commit('SET_TENANT_NAME', tenantName)
    },
    setTimeFormat ({ commit }, timeFormat) {
      commit('SET_TIME_FORMAT', timeFormat)
    },
    getClientRoleList ({
      commit
    },tenantCode) {
      return new Promise((resolve, reject) => {
        Vue.prototype.$interface.system.getClientRoleListApi({tenantCode: tenantCode}).then(({
          data: response
        }) => {
          if (response.code !== 0) {
            throw response.msg
          }
          const list = response.data || {}
          commit('SET_CLIENTROLELIST', list)

          resolve(list)
        }).catch(error => {
          reject(error)
        })
      })
    }
  }
}
